home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 8365 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.0 KB

  1. Path: keats.ugrad.cs.ubc.ca!not-for-mail
  2. From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
  3. Newsgroups: comp.lang.c,comp.lang.c++
  4. Subject: Re: GPF in BC++ 4.5
  5. Date: 16 Feb 1996 20:40:28 -0800
  6. Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
  7. Message-ID: <4g3m7sINNsbb@keats.ugrad.cs.ubc.ca>
  8. References: <4g2oqf$sjc@nntp5.u.washington.edu>
  9. NNTP-Posting-Host: keats.ugrad.cs.ubc.ca
  10.  
  11. In article <4g2oqf$sjc@nntp5.u.washington.edu>,
  12. Todd Stedl <trstedl@u.washington.edu> wrote:
  13.  >I'm having problems with a General Protection Exception that I 
  14.  >continually get from a program I run in Borland C++ 4.5.  The reason I'm 
  15.  >cross posting to the C group is b/c it's a standard C program.  I've 
  16.  >looked at Borland's Web site but found no help there.
  17.  >
  18.  >Here's the basics of the program:
  19.  >I use a lot of arrays and need to write to 2 separate data files, so I 
  20.  >allocate the memory accordingly.  I allocate the the file names as 
  21.  >pointers to char since their names may change depending upon certain 
  22.  >parameters in the program.
  23.  >    char *over_name, *data_name;
  24.  >
  25.  >In the main program I reserve memory for the names.
  26.  >    over_name = (char*)malloc(20*sizeof(char));
  27.  >    data_name = ...
  28.  
  29. You don't have to. They could well be declared as:
  30.  
  31.     char over_name[20], data_name[<whatever>];
  32.  
  33. This is another way to reserve memory without requiring a call to the
  34. allocator. If you are never going to change the pointer, don't bother with
  35. malloc.
  36.  
  37.  >
  38.  >And the very last thing I do in main is free the memory.
  39.  >    free(over_name);
  40.  >    etc.
  41.  >
  42.  >At that point I'm also freeing all the arrays I've used, too.
  43.  >
  44.  >Now, whenever the program executes the free(over_name) line, I get the 
  45.  >following runtime error:
  46.  
  47. IT's hard to know what is going on. It could be a bug caused by you that you
  48. don't see (e.g. memory corruption). Try to do some "self help". Did you already
  49. try print the value of the over_name pointer right after you malloc() it and
  50. before you free() it? Did you try using a debugger? Did you try porting your
  51. program to another environment to see how it behaves there?
  52. -- 
  53.  
  54.